home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / dev / misc / evaluate.lha / evaluate.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-19  |  2.1 KB  |  73 lines

  1. /* evaluate.h (C) 2000 Kyzer/CSG. */
  2. /* Released under the terms of the GNU General Public Licence version 2. */
  3.  
  4. #include <stddef.h>
  5. #include <stdlib.h>
  6.  
  7. /* private memory header for tracked memory allocation */
  8. struct memh {
  9.   struct memh *next;
  10.   void *ptr;
  11. };
  12.  
  13. /* creates a new memory header for allocating memory */
  14. struct memh *create_mem();
  15.  
  16. /* allocates memory using a particular header */
  17. void *mem_alloc(struct memh *mh, size_t len);
  18.  
  19. /* frees all memory for a particular header */
  20. void free_mem(struct memh *mh);
  21.  
  22.  
  23. #define T_INT    0
  24. #define T_REAL   1
  25.  
  26. /* value */
  27. struct val {
  28.   long   ival; /* if type = T_INT, this is the result */
  29.   double rval; /* if type = T_REAL, this is the result */
  30.   char   type; /* either T_INT or T_REAL */
  31. };
  32.  
  33. /* variable */
  34. struct var {
  35.   struct var *next; /* next variable in table or NULL */
  36.   struct val val;   /* value of variable */
  37.   char   *name;     /* name of variable */
  38. };
  39.  
  40. /* variable table */
  41. struct vartable {
  42.   struct var *first; /* first entry in variable table */
  43.   struct memh *mh;
  44. };
  45.  
  46. /* creates a new variable table (NULL if no memory) */
  47. struct vartable *create_vartable();
  48.  
  49. /* frees a variable table */
  50. void free_vartable(struct vartable *vt);
  51.  
  52. /* gets a variable from a variable table (NULL if not found) */
  53. struct var *get_var(struct vartable *vt, char *name);
  54.  
  55. /* puts a variable into a variable table (NULL if no memory) */
  56. struct var *put_var(struct vartable *vt, char *name, struct val *value);
  57.  
  58.  
  59. /* THE FUNCTION YOU WANT TO CALL */
  60.  
  61. /* given a string to evaluate (not NULL), a result to put the answer in
  62.  * (not NULL) and optionally your own variable table (NULL for 'internal
  63.  * only' vartable), will return an error code (and result, etc)
  64.  */
  65. int Calculate(char *eval, struct val *result, struct vartable *variables);
  66.  
  67. /* errors */
  68. #define RESULT_OK               0       /* all OK                       */
  69. #define ERROR_SYNTAX            2       /* invalid expression           */
  70. #define ERROR_VARNOTFOUND       3       /* variable not found           */
  71. #define ERROR_NOMEM             8       /* not enough memory available  */
  72. #define ERROR_DIV0              9       /* division by zero             */
  73.